/* Author Christian Greweling
 * Zommt und setzt marker anhand der Aktuellen Position
 * der Internetverbindung
 * 
 */
var gmapdata;
var gmapmarker;
var infoWindow;

var def_zoomval = 13    ;
var def_longval = 8.04718;
var def_latval = 52.27991;
var browserSupportFlag =  new Boolean();

function if_gmap_init()
{   
    if_gmap_setGeo();
    
    
    var curpoint =  new google.maps.LatLng(def_latval,def_longval);
 
    gmapdata = new google.maps.Map(document.getElementById("mapitems"), {
        center: curpoint,
        zoom: def_zoomval,
        mapTypeId: 'roadmap',
        streetViewControl: false
        
    });
    setTimeout(function()
    
    {
        var icon = new google.maps.MarkerImage("/resources/images/location_new.png");
        gmapmarker = new google.maps.Marker({
                map: gmapdata,
                animation: google.maps.Animation.DROP,
                title: 'Nice Place',
                icon: icon, 
                position: curpoint
            });
        }, 1000);

    infoWindow = new google.maps.InfoWindow;
    google.maps.event.addListener(gmapdata, 'click', function(event) {
        document.getElementById("lang").value = event.latLng.lng().toFixed(6);
        document.getElementById("lat").value = event.latLng.lat().toFixed(6);
        gmapmarker.setPosition(event.latLng);
            streetViewControl: false;
        if_gmap_updateInfoWindow();
    });

    google.maps.event.addListener(gmapmarker, 'click', function() {
        if_gmap_updateInfoWindow();
        infoWindow.open(gmapdata, gmapmarker);
    });

    document.getElementById("lang").value = def_longval;
    document.getElementById("lat").value = def_latval;

    return false;
} // end of if_gmap_init


function if_gmap_loadpicker()
{
    var longval = document.getElementById("lang").value;
    var latval = document.getElementById("lat").value;

    if (longval.length > 0) {
        if (isNaN(parseFloat(longval)) == true) {
            longval = def_longval;
        } // end of if
    } else {
        longval = def_longval;
    } // end of if

    if (latval.length > 0) {
        if (isNaN(parseFloat(latval)) == true) {
            latval = def_latval;
        } // end of if
    } else {
        latval = def_latval;
    } // end of if

    var curpoint = new google.maps.LatLng(latval,longval);

    gmapmarker.setPosition(curpoint);
    gmapdata.setCenter(curpoint);
    //gmapdata.setZoom(zoomval);

    if_gmap_updateInfoWindow();
    return false;
} // end of if_gmap_loadpicker




function if_gmap_updateInfoWindow()
{
    infoWindow.setContent("Longitude: "+ gmapmarker.getPosition().lng().toFixed(6)+"<br>"+"Latitude: "+ gmapmarker.getPosition().lat().toFixed(6));
} // end of if_gmap_bindInfoWindow


function if_gmap_setGeo(){
    // Try W3C Geolocation (Preferred)
    if(navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) {
            initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            gmapdata.setCenter(initialLocation);
            gmapmarker.setPosition(initialLocation); 
            def_longval=gmapmarker.getPosition().lng();
            def_latval=gmapmarker.getPosition().lat();    
        }, function() {
            handleNoGeolocation(browserSupportFlag);
        });
    // Try Google Gears Geolocation
    } else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
            initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
            gmapdata.setCenter(initialLocation);
            gmapmarker.setPosition(initialLocation);
            def_longval=gmapmarker.getPosition().lng();
            def_latval=gmapmarker.getPosition().lat();    
        }, function() {
            handleNoGeoLocation(browserSupportFlag);
        });
    // Browser doesn't support Geolocation
    } else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
  
    function handleNoGeolocation(errorFlag) {
        if (errorFlag == true) {
            alert("Geolocation service failed.");
    
        } else {
            alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
    
        }
    }
}